<html>
<?php include 'includes/header-one.php'; ?>
<body>
<?php include 'includes/header-two.php'; ?>
<form name="form" action="search.php" method="get">
  <input type="text" name="q" />
  <input type="submit" name="Submit" value="Search" />
</form>
<?php
require_once 'includes/lookup.php';
require_once 'includes/config.php';
  // Get the search variable from URL

  $var = @$_GET['q'] ;
  $trimmed = trim($var); //trim whitespace from the stored variable

// rows to return
$limit=10; 

// check for an empty string and display a message.
if ($trimmed == "")
  {
  echo "<p>Please enter a search...</p>";
  include 'includes/footer.php';
  exit;
  }

// check for a search parameter
if (!isset($var))
  {
  echo "<p>We dont seem to have a search parameter!</p>";
  include 'includes/footer.php';
  exit;
  }

// Build SQL Query  
$query = "select * from ".URL_TABLE." where url like \"%$trimmed%\"  
  order by url"; // EDIT HERE and specify your table and field names for the SQL query

 $results=dblookup($query);
 $numrows=mysql_num_rows($results);

// If we have no results, offer a google search as an alternative

if ($numrows == 0)
  {
  echo "<h4>Results</h4>";
  echo "<p>Sorry, your search: &quot;" . $trimmed . "&quot; returned zero results</p>";
  }

// next determine if s has been passed to script, if not use 0
  if (empty($s)) {
  $s=0;
  }

// display what the person searched for
echo "<p>You searched for: &quot;" . $var . "&quot;</p>";

// begin to show results set
echo "Results<br />";
$count = 1 + $s ;

// now you can display thse results returned
  echo '<center><table style="background-color:#bbd8d8; border: solid 1px #000"><tr><th>#</th><th>Short URL</th><th>Long URL</th><th># of Visits</th></tr>';
  mysql_data_seek($results,$s);
  while ($count<=$s+$limit && $count<=$numrows) {
  	$row= mysql_fetch_array($results);
  	$url = $row["url"];
  	$shortid = $row["id"];
	$visits = $row["visited"];
	echo "<tr><td>$count.)&nbsp;</td><td><a href=\"".INSTALL_PATH."$shortid\">".INSTALL_PATH."$shortid</a></td><td>$url</td><td>$visits</td></tr>" ;
	$count++ ;
	}
  echo '</table></center>';

  $currPage = (($s/$limit) + 1);

//break before paging
	echo "<br />";

// next we need to do the links to other results
  if ($s>=1) { // bypass PREV link if s is 0
  	$prevs=($s-$limit);
  	print "&nbsp;<a href=\"$PHP_SELF?s=$prevs&q=$var\">&lt;&lt; 
  	Prev $limit</a>&nbsp&nbsp;";
  	}

// calculate number of pages needing links
  $pages=intval($numrows/$limit);

// $pages now contains int of pages needed unless there is a remainder from division

  if ($numrows%$limit) {
  // has remainder so add one page
  $pages++;
  }

// check to see if last page
  if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {

  // not last page so give NEXT link
  $news=$s+$limit;

  echo "&nbsp;<a href=\"$PHP_SELF?s=$news&q=$var\">Next $limit &gt;&gt;</a>";
  }

$a = $s + ($limit) ;
  if ($a > $numrows) { $a = $numrows ; }
  $b = $s + 1 ;
  echo "<p>Showing results $b to $a of $numrows</p>";
  
?>
<?php include 'includes/footer.php'; ?>
</body>
</html>
